package com.arlen.centre.service.impl;

import com.alibaba.fastjson.JSON;
import com.arlen.centre.service.WxTemplateMesService;
import com.arlen.commom.constant.GetHttpConstant;
import com.arlen.commom.dto.WxTemplateDto;
import com.arlen.until.http.Interaction;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WxTemplateMesServiceImpl implements WxTemplateMesService {
    private static String templateUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";

    private final Logger logger = LoggerFactory.getLogger(WxTemplateMesServiceImpl.class);

    @Autowired
    private Interaction interaction;

    /**
     * @param industry_id1 行业编号1
     * @param industry_id2 行业编号2
     * @param token        access_token
     * @return void
     * @Author Arlen
     * @Description 设置所属行业
     * @Date 7:01 下午 2021/4/25
     * @Param
     **/
    @Override
    public String createIndustry(@NotNull String industry_id1, @NotNull String industry_id2, @NotNull String token) {
        Map<String, String> templateMap = new HashMap<>();
        templateMap.put("industry_id1", industry_id1);
        templateMap.put("industry_id2", industry_id2);
        String jsonT = JSON.toJSONString(templateMap);
        templateUrl = templateUrl.replace("ACCESS_TOKEN", token);
        try {
            boolean b = interaction.sendPost(templateUrl, jsonT);
            if (b) return "win";
            else return "create template fail";
        } catch (IOException e) {
            logger.error("调用httpPost方法异常", ThreadLocal.withInitial(IOException::new));
            return "create template fail";
        }


    }


    private static String getIndustryUrl = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";

    /**
     * @return java.lang.String
     * @Author Arlen
     * @Description 获取所属行业信息
     * @Date 7:31 下午 2021/4/25
     **/
    @Override
    public String getIndustry(@NotNull String token) {
        getIndustryUrl = getIndustryUrl.replace("ACCESS_TOKEN", token);
        String mes = null;
        try {
            mes = interaction.sendGet(getIndustryUrl, GetHttpConstant.NOANALYSIS.getState());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mes;
    }


    private static String listTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";

    //获取所有的模版
    @Override
    public List<WxTemplateDto> listTemplate(@NotNull String token) {
        listTemplateUrl = listTemplateUrl.replace("ACCESS_TOKEN", token);
        String josnTemplate = null;
        try {
            josnTemplate = interaction.sendGet(listTemplateUrl, GetHttpConstant.NOANALYSIS.getState());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNoneBlank(josnTemplate)) { //todo 可以获取数据但获取不到行业信息
            HashMap<String, List<WxTemplateDto>> templateMap = new HashMap<>();
            templateMap = JSON.parseObject(josnTemplate, templateMap.getClass());
            return templateMap.get("template_list");
        }
        return null;

    }

}
